<?php
require_once 'OpModel.php';

/**
 * 表模型，封装了对单张表格的基本操作
 * @author wangjiajun
 *
 */
class OpTableModel extends OpModel
{
    private $_db;
    private $_table;
    
    /**
     * 构造函数
     * @param OpDbModel $db
     * @param string $table
     * @param Log $logger PEAR::Log对象
     */
    function __construct($db, $table, $logger = null)
    {
        parent::__construct($logger);
        $this->_db = $db;
        $this->setTable($table);
    }
    
    /**
     * 设置表名
     * @param string $table
     * @return boolean
     */
    public function setTable($table)
    {
        $this->_table = $table;
        return true;
    }
    
    /**
     * 获取表名
     * @return string
     */
    public function getTable()
    {
        return $this->_table;
    }
    
    /**
     * 插入单行
     * @param array $row
     * @param array $rawRow
     * @param boolean $returnId
     * @return int|boolean
     */
    public function insert($row, $rawRow = null, $returnId = true)
    {
        if (empty($row)) {
            $this->log("empty row");
            return false;
        }
        $sql = "INSERT INTO {$this->_table}". getSqlInsert($row, $rawRow);
        if ($this->_db->exec($sql) === false) {
            $this->log("exec failed");
            return false;
        }
        return $returnId ? $this->_db->getLastInsertId() : true;
    }
    
    /**
     * 查询多行
     * @param array $where
     * @param int $offset
     * @param int $count
     * @param string $sort
     * @param array $otherWhere
     * @param array $columns
     * @return array|boolean
     */
    public function get($where, $offset = 0, $count = null, $sort = null, $otherWhere = null, $columns = null)
    {
        if (is_array($columns)) {
            $columns = implode(',', $columns);
        } else {
            $columns = "*";
        }
        $sql = "SELECT $columns FROM {$this->_table}" . getSqlCond($where, 
            $offset, $count, $sort, $otherWhere);
        $result = $this->_db->query($sql);
        if ($result === false) {
            $this->log("query failed");
            return false;
        }
        return $result;
    }
    
    /**
     * 查询总数
     * @param array $where
     * @param array $otherWhere
     * @return int|boolean
     */
    public function getCount($where, $otherWhere = null)
    {
        $sql = "SELECT COUNT(*) FROM {$this->_table}" . getSqlCond($where, null, null, null, $otherWhere);
        $count = $this->_db->queryOne($sql);
        if ($count === false) {
            $this->log("queryOne failed");
            return false;
        }
        return (int) $count;
    }
    
    /**
     * 查询单行
     * @param array $where
     * @param array $otherWhere
     * @param array|null $columns
     * @return array|null|boolean
     */
    public function getRow($where, $otherWhere = null, $columns = null)
    {
        $result = $this->get($where, 0, null, null, $otherWhere = null, $columns);
        if ($result === false) {
            return false;
        }
        return $result ? $result[0] : null;
    }
    
    /**
     * 查询第一行第一列
     * @param array $where
     * @param array $otherWhere
     * @param string|null $column
     * @return string|null|boolean
     */
    public function getOne($where, $otherWhere = null, $column = null)
    {
        if (is_null($column)) {
            $columns = null;
        } else {
            $columns = array($column);
        }
        $result = $this->get($where, 0, 1, null, $otherWhere = null, $columns);
        if ($result === false) {
            return false;
        }
        return $result ? current($result[0]) : null;
    }
    
    /**
     * 更新
     * @param array $where
     * @param array $row
     * @param array $rawRow
     * @param array $otherWhere
     * @return boolean
     */
    public function update($where, $row, $rawRow = null, $otherWhere = null)
    {
        if (empty($where)) {
            $this->log("empty where");
            return false;
        }
        $sql = "UPDATE {$this->_table}" . getSqlUpdate($row, $rawRow) . getSqlCond($where, 0, null, null, $otherWhere);
        if ($this->_db->exec($sql) === false) {
            $this->log("exec failed");
            return false;
        }
        return true;
    }
    
    /**
     * 删除
     * @param array $where
     * @param array $otherWhere
     * @return boolean
     */
    public function delete($where, $otherWhere = null)
    {
        if (empty($where)) {
            $this->log("empty where");
            return false;
        }
        $sql = "DELETE FROM {$this->_table}" . getSqlCond($where, 0, null, null, $otherWhere);
        if ($this->_db->exec($sql) === false) {
            $this->log("exec failed");
            return false;
        }
        return true;
    }
    
    /**
     * 执行查询语句
     * @param string $sql
     * @param int $fetchStyle
     * @return array|boolean
     */
    public function query($sql, $fetchStyle = PDO::FETCH_ASSOC)
    {
        return $this->_db->query($sql, $fetchStyle);
    }
    
    /**
     * 查询单行
     * @param string $sql
     * @param int $fetchStyle
     * @return array|null|boolean
     */
    public function queryRow($sql, $fetchStyle = PDO::FETCH_ASSOC)
    {
        return $this->_db->queryRow($sql, $fetchStyle);
    }
    
    /**
     * 查询第一行第一列
     * @param string $sql
     * @return string|null|boolean
     */
    public function queryOne($sql)
    {
        return $this->_db->queryOne($sql);
    }
    
    /**
     * 执行修改语句
     * @param string $sql
     * @param boolean $returnAffectedRows
     * @return boolean|int
     */
    public function exec($sql, $returnAffectedRows = false)
    {
        return $this->_db->exec($sql, $returnAffectedRows);
    }
}